import 'package:dio/dio.dart';

class UserApi {
  final Dio? _dio;
  UserApi(this._dio);

  // 登录接口
  Future<dynamic> login({ String? phone, String? password }) async{
    
    FormData formData = FormData.fromMap({
      "phone": phone,
      "password": password
    });
    print('$formData, 登录接口111');
    print('$password, 登录接口111');
     Response? res = await _dio?.post('/login.json', data: formData);
     print('$res 梅西登录2222');
    // if(res?.data != null) {
    //   return res?.data['content'];
    // } else {
    //   return false;
    // };
  }

  // 获取用户基本信息// 登录接口
  Future<dynamic> getUerInfo({ String? phone, String? password }) async{
    
    // Response? res = await _dio?.get('https://www.dmoe.cc/random.php?return=json',
    FormData formData = FormData.fromMap({
      "phone": phone,
      "password": password
    });
    Response? res = await _dio?.post('/user.json',
      data: formData
    );
    if(res?.data != null) {
      return res?.data['content'];
    } else {
      return null;
    };
  }

  // 获取用户基本信息// 登录接口
  Future<dynamic> logout({ String? phone, String? password }) async{
    // Response? res = await _dio?.get('https://www.dmoe.cc/random.php?return=json',
    FormData formData = FormData.fromMap({
      "phone": phone,
      "password": password
    });
    Response? res = await _dio?.post('/logout.json',
      data: formData
    );
    if(res?.data != null) {
      return res?.data['content'];
    } else {
      return null;
    };
  }

  // 获取购买课程
  Future<dynamic> getCourse({ required int id }) async{
    // Response? res = await _dio?.get('https://www.dmoe.cc/random.php?return=json',
   
    Response? res = await _dio?.get('/boughtCourse.json',
      queryParameters: {
        'courseId': id
      }
    );
    if(res?.data != null) {
      return res?.data['content'];
    } else {
      return null;
    };
  }

  /// 发起支付
    Future<dynamic> createPay({
      required String orderNo,
      required int channel,
      String returnUrl = 'http://edufront.lagou.com'
    }) async {
    Map payData = {
      "goodsOrderNo": orderNo,
      "channel": channel == 1 ? 'weChat' : 'aliPay',
      "returnUrl": returnUrl
    };
    Response? res = await _dio?.post('/front/pay/saveOrder', data: payData);
      if (res?.data != null) {
        return res?.data['content'];
      } else {
        return false;
      }
    }
  
}


  
